home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-04 | 4.4 KB | 163 lines | [TEXT/KAHL] |
- /********************************************************* DEFINITION
- DATE: 10/20/93
- AUTHOR: Eric R. Rosé
-
- CLASS: CPPConnectTask
-
- SUPERCLASS: CPPPeriodicTask
-
- This C++ class lets you open a connection with another
- network entity
-
- ********************************************************************/
-
- #include <CPPConnectTask.h>
- #include <CPPTaskManager.h>
- #include <CPPNodeInfo.h>
- #include <MemoryTools.h>
- #include <script.h>
- #include <StringTools.h>
-
- /*-----------------------------------------------------------------*/
- /*------------------------ PUBLIC METHODS -------------------------*/
- /*-----------------------------------------------------------------*/
-
- CPPConnectTask::CPPConnectTask (CPPTaskManager *TaskManager,
- long minPeriod,
- Boolean deleteWhenDone) :
- CPPPeriodicTask (TaskManager, minPeriod,
- deleteWhenDone)
- {
- connectTo = NULL;
- startRec = NULL;
- location = NULL;
- portRec = NULL;
- }
-
- /*-----------------------------------------------------------------*/
-
- CPPConnectTask::~CPPConnectTask (void)
- {
- NukePtr(location);
- NukePtr(startRec);
- NukePtr(portRec);
- }
-
- /*-----------------------------------------------------------------*/
-
- char *CPPConnectTask::ClassName (void)
- {
- return "CPPConnectTask";
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPConnectTask::DoPeriodicAction (void)
- /* store the error code and session ID which was returned */
- {
- if (this->startRec->ioResult != 1)
- {
- this->callResult = startRec->ioResult;
- this->hasCompleted = TRUE;
- if (this->callResult == noErr)
- this->sessionID = this->startRec->sessRefNum;
- }
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPConnectTask::DoCompletedAction (void)
- {
- CPPPeriodicTask::DoCompletedAction();
- NukePtr(location);
- NukePtr(portRec);
- NukePtr(startRec);
- }
-
- /*-----------------------------------------------------------------*/
-
- PPCSessRefNum CPPConnectTask::GetSessionID (Boolean *isDone)
- {
- if ((*isDone = this->hasCompleted) == TRUE)
- return this->sessionID;
- else
- return 0;
- }
-
- /*-----------------------------------------------------------------*/
-
- void CPPConnectTask::StartConnectTask (PPCPortRefNum SourcePortRefNum,
- CPPNodeInfo *ConnectTo,
- CompletionProc DoProc)
- {
- StringPtr ObjStr = NULL, TypeStr = NULL, ZoneStr = NULL;
- EntityName theName;
- StringPtr PName;
- short i = 1;
-
- if (!this->hasCompleted)
- return;
-
- this->sessionID = 0;
-
- NukePtr(startRec);
- connectTo = ConnectTo;
-
- startRec = (PPCStartPBPtr)NewPtrClear(sizeof(PPCStartPBRec));
- if (startRec)
- {
- location = (LocationNamePtr)NewPtrClear(sizeof(LocationNameRec));
- if (location)
- {
- portRec = (PortInfoRec *)NewPtrClear(sizeof(PortInfoRec));
- if (portRec)
- {
- PName = portRec->name.name;
- // fill in the Location Rec with data from the SendTo object;
- ConnectTo->GetNodeName (&ObjStr, &TypeStr, &ZoneStr);
- CopyString (ObjStr, theName.objStr);
- CopyString (TypeStr, theName.typeStr);
- CopyString (ZoneStr, theName.zoneStr);
- location->locationKindSelector = ppcNBPLocation;
- location->u.nbpEntity = theName;
-
- // fill in the PPCPortRec with the destination port name
- portRec->name.nameScript = smRoman;
-
- CopyString(TypeStr, PName);
- while (((PName[i] & 0x00FF) != 0xA5) && (i <= PName[0]))
- i++;
- PName[0] = i-1;
- portRec->name.portKindSelector = ppcByString;
- CopyString ("\pPPCCommPort", portRec->name.u.portTypeStr);
-
- // fill in the 'start session' record
- startRec->ioCompletion = NULL;
- startRec->portRefNum = SourcePortRefNum;
- startRec->serviceType = ppcServiceRealTime;
- startRec->resFlag = 0;
- startRec->portName = &portRec->name;
- startRec->locationName = location;
- startRec->userData = 0;
- startRec->userRefNum = 0; // guest access
-
- // Try to open the connection asynchronously
- if ((this->callResult = PPCStart (startRec, TRUE)) == noErr)
- { // add the task, if the call does not error-out
- this->hasCompleted = FALSE;
- this->ourManager->AddPeriodicTask(this);
- return;
- }
- }
- }
- }
-
- // we will only get here if all memory allocations fail, or
- // the call itself fails
- NukePtr(startRec);
- NukePtr(location);
- NukePtr(portRec);
- }
-
- /*-----------------------------------------------------------------*/
-